package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.pojo.TbSpecParam;
import com.leyou.item.mapper.TbSpecParamMapper;
import com.leyou.item.service.TbSpecParamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 规格参数组下的参数名 服务实现类
 * </p>
 *
 * @author leyou
 * @since 2020-02-29
 */
@Service
public class TbSpecParamServiceImpl extends ServiceImpl<TbSpecParamMapper, TbSpecParam> implements TbSpecParamService {

    @Autowired
    private TbSpecParamMapper tbSpecParamMapper;

    /**
     * 根据组id 查询对应的组下规格参数
     *
     * @param groupId
     * @param cid
     * @param searching
     * @return
     */
    @Override
    public List<SpecParamDTO> findSpecParam(Long groupId, Long cid, Boolean searching) {
        QueryWrapper<TbSpecParam> queryWrapper = new QueryWrapper<>();
        if (groupId != null && groupId != 0) {
            queryWrapper.lambda().eq(TbSpecParam::getGroupId, groupId);
        }
        if (cid != null && cid != 0) {
            queryWrapper.lambda().eq(TbSpecParam::getCid, cid);
        }
        if (searching != null) {
            queryWrapper.lambda().eq(TbSpecParam::getSearching, searching);
        }
        List<TbSpecParam> specParamList = tbSpecParamMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(specParamList)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specParamList, SpecParamDTO.class);
    }
}
